using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;

namespace QuoteAdvantageEditor
{
	/// <summary>
	/// Summary description for LoadCrmProductDlg.
	/// </summary>
	public class LoadCrmProductDlg : System.Windows.Forms.Form
	{
      private AuthoringEngine eng = null;
      private Product objSelectedProduct = null;
      private System.Windows.Forms.Button btnOK;
      private System.Windows.Forms.Button btnCancel;
      private System.Windows.Forms.Label lblProductId;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.Panel panel1;
      private System.Windows.Forms.Button btnSearchDescription;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Label label3;
      private System.Windows.Forms.Button btnSearchPartNumber;
      private System.Windows.Forms.ListView ctlProductList;
      private System.Windows.Forms.ColumnHeader colName;
      private System.Windows.Forms.ColumnHeader colProductId;
      private System.Windows.Forms.TextBox txtDescription;
      private System.Windows.Forms.TextBox txtPartNumber;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
      
      public AuthoringEngine Engine
      {
         get { return eng; }
         set { eng = value; }
      }
      
      public Product SelectedProduct
      {
         get { return objSelectedProduct; }
         set { objSelectedProduct = value; }
      }

		public LoadCrmProductDlg()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
         this.btnOK = new System.Windows.Forms.Button();
         this.btnCancel = new System.Windows.Forms.Button();
         this.txtDescription = new System.Windows.Forms.TextBox();
         this.lblProductId = new System.Windows.Forms.Label();
         this.btnSearchDescription = new System.Windows.Forms.Button();
         this.label2 = new System.Windows.Forms.Label();
         this.panel1 = new System.Windows.Forms.Panel();
         this.label1 = new System.Windows.Forms.Label();
         this.label3 = new System.Windows.Forms.Label();
         this.txtPartNumber = new System.Windows.Forms.TextBox();
         this.btnSearchPartNumber = new System.Windows.Forms.Button();
         this.ctlProductList = new System.Windows.Forms.ListView();
         this.colName = new System.Windows.Forms.ColumnHeader();
         this.colProductId = new System.Windows.Forms.ColumnHeader();
         this.SuspendLayout();
         // 
         // btnOK
         // 
         this.btnOK.Location = new System.Drawing.Point(224, 360);
         this.btnOK.Name = "btnOK";
         this.btnOK.Size = new System.Drawing.Size(80, 23);
         this.btnOK.TabIndex = 10;
         this.btnOK.Text = "OK";
         this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
         // 
         // btnCancel
         // 
         this.btnCancel.Location = new System.Drawing.Point(312, 360);
         this.btnCancel.Name = "btnCancel";
         this.btnCancel.Size = new System.Drawing.Size(80, 23);
         this.btnCancel.TabIndex = 11;
         this.btnCancel.Text = "Cancel";
         this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
         // 
         // txtDescription
         // 
         this.txtDescription.Location = new System.Drawing.Point(32, 48);
         this.txtDescription.Name = "txtDescription";
         this.txtDescription.Size = new System.Drawing.Size(296, 21);
         this.txtDescription.TabIndex = 3;
         this.txtDescription.Text = "";
         this.txtDescription.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtDescription_KeyPress);
         // 
         // lblProductId
         // 
         this.lblProductId.Location = new System.Drawing.Point(8, 8);
         this.lblProductId.Name = "lblProductId";
         this.lblProductId.Size = new System.Drawing.Size(248, 16);
         this.lblProductId.TabIndex = 1;
         this.lblProductId.Text = "Search the Microsoft CRM Product Catalog";
         // 
         // btnSearchDescription
         // 
         this.btnSearchDescription.Location = new System.Drawing.Point(248, 72);
         this.btnSearchDescription.Name = "btnSearchDescription";
         this.btnSearchDescription.Size = new System.Drawing.Size(80, 23);
         this.btnSearchDescription.TabIndex = 4;
         this.btnSearchDescription.Text = "Search";
         this.btnSearchDescription.Click += new System.EventHandler(this.btnSearchDescription_Click);
         // 
         // label2
         // 
         this.label2.Location = new System.Drawing.Point(32, 32);
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size(200, 16);
         this.label2.TabIndex = 2;
         this.label2.Text = "For Product Descriptions that contain: ";
         // 
         // panel1
         // 
         this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
         this.panel1.Location = new System.Drawing.Point(8, 192);
         this.panel1.Name = "panel1";
         this.panel1.Size = new System.Drawing.Size(384, 4);
         this.panel1.TabIndex = 13;
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(8, 200);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(248, 16);
         this.label1.TabIndex = 8;
         this.label1.Text = "Search Results:";
         // 
         // label3
         // 
         this.label3.Location = new System.Drawing.Point(32, 112);
         this.label3.Name = "label3";
         this.label3.Size = new System.Drawing.Size(200, 16);
         this.label3.TabIndex = 5;
         this.label3.Text = "For Part Numbers that contain:";
         // 
         // txtPartNumber
         // 
         this.txtPartNumber.Location = new System.Drawing.Point(32, 128);
         this.txtPartNumber.Name = "txtPartNumber";
         this.txtPartNumber.Size = new System.Drawing.Size(296, 21);
         this.txtPartNumber.TabIndex = 6;
         this.txtPartNumber.Text = "";
         this.txtPartNumber.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtPartNumber_KeyPress);
         // 
         // btnSearchPartNumber
         // 
         this.btnSearchPartNumber.Location = new System.Drawing.Point(248, 152);
         this.btnSearchPartNumber.Name = "btnSearchPartNumber";
         this.btnSearchPartNumber.Size = new System.Drawing.Size(80, 23);
         this.btnSearchPartNumber.TabIndex = 7;
         this.btnSearchPartNumber.Text = "Search";
         this.btnSearchPartNumber.Click += new System.EventHandler(this.btnSearchPartNumber_Click);
         // 
         // ctlProductList
         // 
         this.ctlProductList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.ctlProductList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                                                                                         this.colName,
                                                                                         this.colProductId});
         this.ctlProductList.FullRowSelect = true;
         this.ctlProductList.Location = new System.Drawing.Point(8, 216);
         this.ctlProductList.MultiSelect = false;
         this.ctlProductList.Name = "ctlProductList";
         this.ctlProductList.Size = new System.Drawing.Size(384, 136);
         this.ctlProductList.TabIndex = 9;
         this.ctlProductList.View = System.Windows.Forms.View.Details;
         this.ctlProductList.DoubleClick += new System.EventHandler(this.ctlProductList_DoubleClick);
         // 
         // colName
         // 
         this.colName.Text = "Name";
         this.colName.Width = 253;
         // 
         // colProductId
         // 
         this.colProductId.Text = "Product ID";
         this.colProductId.Width = 108;
         // 
         // LoadCrmProductDlg
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
         this.ClientSize = new System.Drawing.Size(402, 392);
         this.Controls.Add(this.ctlProductList);
         this.Controls.Add(this.btnSearchPartNumber);
         this.Controls.Add(this.txtPartNumber);
         this.Controls.Add(this.label3);
         this.Controls.Add(this.label1);
         this.Controls.Add(this.panel1);
         this.Controls.Add(this.label2);
         this.Controls.Add(this.btnSearchDescription);
         this.Controls.Add(this.lblProductId);
         this.Controls.Add(this.txtDescription);
         this.Controls.Add(this.btnCancel);
         this.Controls.Add(this.btnOK);
         this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "LoadCrmProductDlg";
         this.ShowInTaskbar = false;
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Text = "Search Microsoft CRM";
         this.Load += new System.EventHandler(this.LoadCrmProductDlg_Load);
         this.ResumeLayout(false);

      }
		#endregion

      
      private void LoadCrmProductDlg_Load(object sender, System.EventArgs e)
      {
      
      }


      private void btnOK_Click(object sender, System.EventArgs e)
      {
         CloseDialog();
      }

      private void btnCancel_Click(object sender, System.EventArgs e)
      {
         this.Close();      
      }

      private void btnSearchDescription_Click(object sender, System.EventArgs e)
      {
         Cursor oldCursor = Cursor.Current;
         Cursor.Current = Cursors.WaitCursor; 
         
         if (txtDescription.Text == "")
         {
            MessageBox.Show(this, "Please enter a description", "QuoteAdvantage Editor");
         }
         else
         {    
            ArrayList   objResultList = eng.LoadProductNameLike(txtDescription.Text);

            RefreshResultList(objResultList);
         }

         Cursor.Current = oldCursor;
      }

      private void btnSearchPartNumber_Click(object sender, System.EventArgs e)
      {
         Cursor oldCursor = Cursor.Current;
         Cursor.Current = Cursors.WaitCursor; 
         
         if (txtPartNumber.Text == "")
         {
            MessageBox.Show(this, "Please enter a part number", "QuoteAdvantage Editor");
         }
         else
         {    
            ArrayList   objResultList = eng.LoadProductNumberLike(txtPartNumber.Text);

            RefreshResultList(objResultList);
         }

         Cursor.Current = oldCursor;
      }

      private void RefreshResultList(ArrayList objResultList)
      {
         ctlProductList.Items.Clear();

         if (objResultList.Count == 0)
         {
            MessageBox.Show(this, "No matches were found.", "QuoteAdvantage Editor");
         }
         else
         {
            foreach (Product objProduct in objResultList)
            {
               ListViewItem   objItem = new ListViewItem(objProduct.Name, 1);

               objItem.Tag = objProduct;
               objItem.SubItems.Add(objProduct.ProductNumber);
               ctlProductList.Items.Add(objItem);
            }
         }
      }

      private void ctlProductList_DoubleClick(object sender, System.EventArgs e)
      {
         CloseDialog();
      }

      private void CloseDialog()
      {
         if (ctlProductList.SelectedItems.Count == 0)
         {
            MessageBox.Show(this, "Please use the Search buttons to locate a product in Microsoft CRM", "QuoteAdvantage");
         }
         else
         {
            objSelectedProduct = (Product)ctlProductList.SelectedItems[0].Tag;
            this.DialogResult = DialogResult.OK;
            this.Close();
         }
      }                 
      
      private void txtDescription_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
      {
         if (e.KeyChar == 13)
         {
            btnSearchDescription_Click(this, null);  
         }
      }

      private void txtPartNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
      {
         if (e.KeyChar == 13)
         {
            btnSearchPartNumber_Click(this, null);  
         }
      }


	} // end class
}
